package cn.practice._05mvcboot02.controller;

import cn.practice._05mvcboot02.common.response.JsonResult;
import cn.practice._05mvcboot02.common.response.StatusCode;
import cn.practice._05mvcboot02.pojo.dto.UserArticleDTO;
import cn.practice._05mvcboot02.pojo.dto.UserLoginDTO;
import cn.practice._05mvcboot02.pojo.entity.Article;
import cn.practice._05mvcboot02.pojo.mapper.UserMapper;
import cn.practice._05mvcboot02.pojo.vo.ArticleVO;
import cn.practice._05mvcboot02.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.Size;

@Slf4j
@RestController
@Api(tags = "用户模块")
@RequestMapping("/v1/users/")
@Validated
public class UserController {

    // 自动装配
    @Autowired
    private UserMapper userMapper;

    // 登录接口
    @PostMapping("login")
    @ApiOperation(value = "用户登录")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session){
        log.info("用户登录信息：{}", userLoginDTO);

        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();
        // 登录查询接口
        UserVO userVO = userMapper.selectByUsernameAndPassword(username,password);
        if (userVO == null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        // 登录成功获取session会话
        session.setAttribute("user", userVO);
        return JsonResult.success(userVO);
    }

    // 发表文章功能
    @ApiOperation("发布文章")
    @PostMapping("article")
    public JsonResult article(@RequestBody @Validated UserArticleDTO userArticleDTO, @ApiIgnore HttpSession session ){

        log.debug("用户发表文章信息：{}", userArticleDTO);
        // 获取session会话
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Article article = new Article();
        article.setUserId(userVO.getId());
        BeanUtils.copyProperties(userArticleDTO, article);

        int rows = userMapper.insertArticle(article);
        if (rows > 0){
            return JsonResult.success();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);

    }

    // 查询指定文章功能
    @ApiOperation("查询指定文章")
    @GetMapping("selectByArticleId")
    public JsonResult selectByArticleId(@RequestParam @Range(min = 1, max = 100, message = "文章ID必须在1-100之间") Long articleId){
        log.error("查询指定文章信息：{}", articleId);
        ArticleVO  articlevo = userMapper.selectByArticleId(articleId);

        return JsonResult.success(articlevo);
    }


}
